import os
import numpy as np
import sys
import scipy
import math
from PyQt5.QtCore import pyqtSignal,pyqtSlot,QThread
from PyQt5.QtWidgets import QMainWindow,QApplication,QInputDialog,QFileDialog,\
    QMessageBox,QColorDialog,QListWidgetItem,QTableWidgetItem,QProgressBar,\
    QRadioButton,QMdiSubWindow,QWidget,QMenu
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.backends.backend_template import FigureCanvas
from scipy.stats import stats
import matplotlib.pyplot as plt
from PyQt5.uic import loadUi


class Form(QWidget):
    def __init__(self):
        super(Form, self).__init__()
        loadUi('satellite.ui', self)
        self.figure=plt.figure()
        self.canvas = FigureCanvasQTAgg(self.figure)
        self.horizontalLayoutSeaClutter.addWidget(self.canvas)
        self.canvas.draw()
        font = {'family': 'SimSun'}
        plt.rc('font', **font)  # 步骤一（设置字体的更多属性）
        plt.rc('axes', unicode_minus=False)

    @pyqtSlot()
    def on_pushButtonreliability_clicked(self):
        self.figure.clear()
        ax = self.figure.add_subplot()
        # altitude = float(self.lineEditAltitude.text()) / 1e3
        # minDistance = float(self.lineEditMinDistance.text())
        # maxDistance = float(self.lineEditMaxDistance.text())
        # distance = np.arange(minDistance, maxDistance + 1, 1)
        dt = int(self.lineEdittotalltime.text())
        t2 = int(self.lineEdittotalltime.text())
        ll = int(self.lineEditinvalidtime.text())
        lc = float(self.lineEditinvalidcrease.text())
        t  = np.linspace(0, dt, t2)
        L = (1/ll) + t * lc/10000000
        R = np.exp(-L * t)
        # plt.plot(t, R)
        ax.plot(t, R)
        ax.grid(True)
        ax.set_xlabel('时间/h')
        ax.set_ylabel('可靠度')
        self.canvas.draw()

    @pyqtSlot()
    def on_pushButtonlife_clicked(self):
        self.figure.clear()
        ax = self.figure.add_subplot()
        dt=int(self.lineEdittotalltime.text())
        t2=int(self.lineEdittotalltime.text())
        ll=int(self.lineEditinvalidtime.text())
        lc=float(self.lineEditinvalidcrease.text())
        t = np.linspace(0, dt, t2)
        L = (1/ll) + t * lc/10000000
        MTTF = 1/L
        ax.plot(t, MTTF)
        ax.grid(True)
        ax.set_xlabel('时间/h')
        ax.set_ylabel('剩余寿命/h')
        self.canvas.draw()

    @pyqtSlot()
    def on_pushButtontuihua_clicked(self):
        self.figure.clear()
        ax = self.figure.add_subplot()
        dt = int(self.lineEdittuihuatime.text())
        tt = int(self.lineEdittuihuatime.text())
        t = np.linspace(0.1,dt,tt)

        mu = float(self.lineEdittuihuamu.text())
        s = float(self.lineEdittuihuasigma.text())
        D = int(self.lineEdittuihuanumber.text())
        A = (D - mu * t)/ (s * np.sqrt(t))
        B = (-D - mu * t)/ (s * np.sqrt(t))
        R = scipy.stats.norm.cdf(A, 0, 1) - np.exp(2 * D * mu / (s*s))* scipy.stats.norm.pdf(B, 0, 1)

        ax.plot(t, R)
        ax.grid(True)
        ax.set_xlabel('时间')
        ax.set_ylabel('可靠度')
        self.canvas.draw()

    @pyqtSlot()
    def on_pushButtonchongfuR_clicked(self):
        self.figure.clear()
        ax = self.figure.add_subplot()
        p = int(self.lineEditenv.text())
        LL = int(self.lineEditchongfulamda.text())
        L = 1 / LL
        TT = int(self.lineEditchongfucishu.text())
        DT = TT
        a = float(self.lineEdityiling.text())
        T = np.linspace(0,DT, TT)
        R = np.exp(-(1 - a) * T * p * L)
        ax.plot(T, R)
        ax.grid(True)
        ax.set_xlabel('重复使用次数')
        ax.set_ylabel('可靠度')
        self.canvas.draw()

    @pyqtSlot()
    def on_pushButtonchongfua_clicked(self):
        self.figure.clear()
        ax = self.figure.add_subplot()
        p = int(self.lineEditenv.text())
        LL = int(self.lineEditchongfulamda.text())
        L = 1 / LL
        T = int(self.lineEditchongfucishu.text())
        a = np.linspace(0,0.99,99)
        R = np.exp(-(1 - a) * T * p * L)
        ax.plot(a, R)
        ax.grid(True)
        ax.set_xlabel('役龄因子')
        ax.set_ylabel('可靠度')
        self.canvas.draw()





if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit(app.exec_())







